package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.TaskMapper;
import com.xgame.server.db.model.Tasklist;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class TasklistCsTool {
    @Resource
    private TaskMapper taskMapper;

    //插入玩家记录
    public void insertTaskList(Game.TaskListCS data) throws Exception {

        deleteTaskList(data);

        Integer result = new Integer(0);

        Game.TaskCS main_task = data.getMainTask();
        Game.TaskCS reward_task = data.getRewardTask();
        List<Game.TaskCS> pub_task = data.getPubTaskList();

        Tasklist tasklist1 = setData(data, main_task, 1, 0);
        Tasklist tasklist2 = setData(data, reward_task, 2, 0);

        taskMapper.insert(tasklist1);
        taskMapper.insert(tasklist2);

        for (int i = 0; i < pub_task.size(); i++) {
            Tasklist tasklist3 = setData(data, pub_task.get(i), 3, i);
            taskMapper.insert(tasklist3);
        }

    }

    //设置数据
    private Tasklist setData(Game.TaskListCS data, Game.TaskCS main_task, Integer type, Integer subtype) throws Exception {
        Tasklist tasklist = new Tasklist();
        tasklist.setAccount(data.getAccount());
        tasklist.setPlayerId(data.getPlayerID());
        tasklist.setIdentify(data.getIdentify());
        tasklist.setCurrentTask(main_task.getCurrentTask());
        tasklist.setType(type);

        List<Game.TaskScheduleOneCS> items = main_task.getScheduleList();
        int length = items.size();
        String tempStr = "";
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                tempStr += main_task.getSchedule(i).getId() + ";" + main_task.getSchedule(i).getHaveCount() + ";" + main_task.getSchedule(i).getCount();
            } else {
                tempStr += main_task.getSchedule(i).getId() + ";" + main_task.getSchedule(i).getHaveCount() + ";" + main_task.getSchedule(i).getCount() + "^";
            }
        }
        tasklist.setSchedule(tempStr);

        List<Integer> finishtask = main_task.getFinishTaskList();
        length = finishtask.size();
        tempStr = "";
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                tempStr += main_task.getFinishTask(i);
            } else {
                tempStr += main_task.getFinishTask(i) + "^";
            }
        }
        tasklist.setFinishTask(tempStr);
        tasklist.setCompleteTaskCount(main_task.getCompleteTaskCount());
        tasklist.setTotalCount(main_task.getTotalCount());
        tasklist.setTodayCount(main_task.getTodayCount());

        return tasklist;
    }

    //删除记录
    public Integer deleteTaskList(Game.TaskListCS data) throws Exception {
        EntityWrapper<Tasklist> user = new EntityWrapper<>();
        user.eq("account", data.getAccount());

        return taskMapper.delete(user);
    }

    //删除记录
    public Integer deleteTask(String account) throws Exception {
        EntityWrapper<Tasklist> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return taskMapper.delete(userWrapper);
    }

    //查询记录
    public List<Tasklist> selectTask(String account) throws Exception {
        EntityWrapper<Tasklist> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return taskMapper.selectList(userWrapper);
    }

}
